<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>树形结构转换</title>
</head>
<body>

</body>
<script>
    const testData = [
        {
            frameName: "Frame 1",
            projectVoList: [
                {
                    projectName: "Project 1.1",
                    modelVoList: [
                        {
                            projectName: "Model 1.1.1"
                        },
                        {
                            projectName: "Model 1.1.2"
                        }
                    ]
                },
                {
                    projectName: "Project 1.2"
                }
            ],
        },
        {
            frameName: "Frame 2",
            projectVoList: [
                {
                    projectName: "Project 2.1"
                }
            ]
        }
    ];
    const testData1 = [
        {
            frameName: "Frame A",
            projectVoList: [
                {
                    projectName: "Project A.1",
                    modelVoList: [
                        {
                            projectName: "Model A.1.1",
                            projectVoList: [
                                {
                                    projectName: "Project A.1.1.1"
                                }
                            ]
                        },
                        {
                            projectName: "Model A.1.2",
                            modelVoList: [
                                {
                                    projectName: "Model A.1.2.1",
                                    projectVoList: [
                                        {
                                            projectName: "Project A.1.2.1.1"
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                },
                {
                    projectName: "Project A.2",
                    modelVoList: [
                        {
                            projectName: "Model A.2.1"
                        }
                    ]
                }
            ]
        },
        {
            frameName: "Frame B",
            projectVoList: [
                {
                    projectName: "Project B.1",
                    modelVoList: [
                        {
                            projectName: "Model B.1.1"
                        }
                    ]
                }
            ]
        }
    ];
    const testData2 = [
        {
            frameName: "Root Frame",
            projectVoList: [
                {
                    projectName: "Project 1",
                    modelVoList: [
                        {
                            projectName: "Model 1.1",
                            projectVoList: [
                                {
                                    projectName: "Project 1.1.1",
                                    modelVoList: [
                                        {
                                            projectName: "Model 1.1.1.1"
                                        }
                                    ]
                                }
                            ]
                        },
                        {
                            projectName: "Model 1.2"
                        }
                    ]
                },
                {
                    projectName: "Project 2",
                    modelVoList: [
                        {
                            projectName: "Model 2.1",
                            projectVoList: [
                                {
                                    projectName: "Project 2.1.1",
                                    modelVoList: [
                                        {
                                            projectName: "Model 2.1.1.1",
                                            projectVoList: [
                                                {
                                                    projectName: "Project 2.1.1.1"
                                                }
                                            ]
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ];


    function transformTree(data) {
        return data.map(node => {
            // 创建一个新的对象来存储转换后的节点
            const transformedNode = {
                label: node.frameName || node.projectName, // 使用 frameName 或 projectName 作为 label
                children: []
            };

            // 检查并处理 projectVoList
            if (node.projectVoList && node.projectVoList.length > 0) {
                transformedNode.children = transformTree(node.projectVoList);
            }

            // 检查并处理 modelVoList
            if (node.modelVoList && node.modelVoList.length > 0) {
                transformedNode.children = [...transformedNode.children, ...transformTree(node.modelVoList)];
            }

            return transformedNode;
        });
    }

    console.log(transformTree(testData));
    console.log(transformTree(testData1));
    console.log(transformTree(testData2));

</script>
</html>
